#include <my_header.h>
#include "train.h"

int main(int argc,char* argv[])
{   
    //初始化,创建数组
    char user_cin[1024]={0};//储存用户输入信息
    char ret_worker[1024]={0};//接受工人线程返回值
    int result;//临时储存,判断是哪个命令
    train_t train;//要发送的结构体

    //连接客户端
    int sfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    int ret_connect=connect(sfd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(ret_connect,-1,"connect filed");
    printf("您好用户，您接入网盘成功\n");
    SYSLOG_PRINTF(LOG_INFO,"用户接入网盘");
    while (1) {
        //用户登录
        printf("请登录你的账号.\n");
        printf("请输入用户名");
        fgets(user_cin, sizeof(user_cin), stdin);
        send(sfd, user_cin, sizeof(user_cin), 0);
        printf("请输入密码");
        bzero(user_cin, sizeof(user_cin));
        fgets(user_cin, sizeof(user_cin), stdin);
        send(sfd, user_cin, sizeof(user_cin), 0);
        //等待服务端返回,成功或者失败
        bzero(user_cin, sizeof(user_cin));
        while (1) {
            ssize_t ret = recv(sfd, &ret_worker, sizeof(ret_worker), 0);
            if (ret > 0) {
                printf("%ld\n", ret);
                break;
            }
        }
        int a = strcmp(ret_worker, "-1");
        if (a == 0) {
            printf("登录 失败请重新登陆");
            SYSLOG_PRINTF(LOG_ERR, "登录 失败");
            continue;
        }
        printf("登录成功");
        break;
    }
    //用户登陆成功,等待用户输入信息
    printf("目前支持:\n cd \n ls \n puts \n gets\n remove \n pwd \n mkdir\n rmdir\n");
    while(1)//用户输入命令 对命令字符串切割，解析，并用TLV发送  当使用pwd等需要接受服务器端返回数据的命令，做接受，并打印在屏幕上
    {   
         bzero(user_cin,sizeof(user_cin));
        bzero(ret_worker,sizeof(ret_worker));
        train.flag=0;
        train.length=0;
        bzero(train.data,sizeof(train.data));

        printf("请输入您的需求\n");
        fgets(user_cin,sizeof(user_cin),stdin);
        SYSLOG_PRINTF(LOG_ERR,user_cin);
        char temp[1024]={0};
        strncpy(temp,user_cin,sizeof(temp));
        user_cin[strcspn(user_cin, "\n")] = '\0';
        char *p=strtok(user_cin," ");
        if((result=strcmp(p,"cd"))==0){
            //先把数据存下来
            train.flag=1;
            p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            //接受返回值
            sleep(1);
            recv(sfd,&ret_worker,sizeof(ret_worker),0);
            
            if(strcmp(ret_worker,"-1")==0){
                printf("cd 失败");
                SYSLOG_PRINTF(LOG_ERR,"cd 失败");
            }
            printf("成功\n");
        }
        else if((result=strcmp(p,"ls"))==0){
            //先把数据存下来
            train.flag=2;
            //p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            //接受返回值
            sleep(1);
            
            recv(sfd,&ret_worker,sizeof(ret_worker),0);
            
            if(strcmp(ret_worker,"-1")==0){
                printf("ls 失败");
                SYSLOG_PRINTF(LOG_ERR,"ls 失败");
                continue;
            }
            printf("%s\n",ret_worker);
            continue;

        }
        else if((result=strcmp(p,"puts"))==0){
            //先把数据存下来
            train.flag=3;
            p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);
            char file_name[50]={0};
            strcat(file_name,p);
            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //printf("%s",train.data);
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            func_puts_client(sfd,file_name);
            sleep(1);
            //接受返回值
            while(1){
                ssize_t ret=recv(sfd,&ret_worker,sizeof(ret_worker),0);
                if(ret>0){
                    //printf("%ld\n",ret);
                    break;
                }
            }
            int a=strcmp(ret_worker,"-1");
            if(a==0){
                printf("puts 失败");
                SYSLOG_PRINTF(LOG_ERR,"puts 失败");
            }
            continue;
        }
        else if((result=strcmp(p,"gets"))==0){
            train.flag=4;
            p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //printf("%s",train.data);
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            func_gets_client(sfd);
            sleep(1);
            //接受返回值
            recv(sfd,&ret_worker,sizeof(ret_worker),0);
            
            if(strcmp(ret_worker,"-1")==0){
                printf("gets 失败");
                SYSLOG_PRINTF(LOG_ERR,"gets 失败");
            }
            printf("成功\n");
            continue;
        }
        else if((result=strcmp(p,"remove"))==0){
            //先把数据存下来
            train.flag=5;
            p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //printf("%s",train.data);
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            sleep(1);
            //接受返回值
            while(1){
                ssize_t ret=recv(sfd,&ret_worker,sizeof(ret_worker),0);
                if(ret>0){
                    //printf("%ld\n",ret);
                    break;
                }
            }
            int a=strcmp(ret_worker,"-1");
            if(a==0){
                printf("remove 失败");
                SYSLOG_PRINTF(LOG_ERR,"remove 失败");
            }
            continue;

        }
        else if((result=strcmp(p,"pwd"))==0){
            //先把数据存下来
            train.flag=6;
            //p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //printf("%s",train.data);
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            sleep(1);
            //接受返回值
            while(1){
                ssize_t ret=recv(sfd,&ret_worker,sizeof(ret_worker),0);
                if(ret>0){
                    //printf("%ld\n",ret);
                    break;
                }
            }
            int a=strcmp(ret_worker,"-1");
            if(a==0){
                printf("pwd 失败");
                SYSLOG_PRINTF(LOG_ERR,"pwd 失败");
                continue;
            }
            //处理返回值
            printf("%s\n",ret_worker);
            continue;

        }

        else if((result=strcmp(p,"mkdir"))==0){
            //先把数据存下来
            train.flag=7;
            p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //printf("%s",train.data);
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            sleep(1);
            //接受返回值
            while(1){
                ssize_t ret=recv(sfd,&ret_worker,sizeof(ret_worker),0);
                //printf("%ld\n",ret);
                if(ret>0){
                    //printf("%ld\n",ret);
                    break;
                }
            }
            printf("%s\n",ret_worker);
            int a=strcmp(ret_worker,"-1");
            if(a==0){
                printf("mkdir 失败");
                SYSLOG_PRINTF(LOG_ERR,"mkdir 失败");
            }
            continue;
        }
        else if((result=strcmp(p,"rmdir"))==0){
            //先把数据存下来
            train.flag=8;
            p=strtok(NULL," ");
            train.length=strlen(p);
            strcpy(train.data,p);

            //检查格式
            p=strtok(NULL," ");
            if(p!=NULL){
                printf("格式错误");
                SYSLOG_PRINTF(LOG_ERR,"格式错误");
                continue;
            }
            //printf("%s",train.data);
            //发送小火车
            send(sfd,&train,sizeof(train),0);
            sleep(1);
            //接受返回值
            while(1){
                ssize_t ret=recv(sfd,&ret_worker,sizeof(ret_worker),0);
                if(ret>0){
                    //printf("%ld\n",ret);
                    break;
                }
            }
            int a=strcmp(ret_worker,"-1");
            if(a==0){
                printf("rmdir 失败");
                SYSLOG_PRINTF(LOG_ERR,"redir 失败");
            }
            printf("删除成功\n");
            continue;

        }
        else{
            printf("您输入格式错误，请重新输入\n");
            SYSLOG_PRINTF(LOG_ERR,"用户输入格式错误");
            continue;
        }


    }
    close(sfd);
    return 0;
}

